﻿using AutoMapper;
using Common.Domain;
using Common.Infrastructure.Repository;
using Common.Services;
using MediatR;

namespace Business.WriteAPI.Application.Products
{
    public class ProductDelRequestHandler : IRequestHandler<ProductDelRequestModel, APIResult>
    {
        private IBaseRepository<Product> productRepository;
        private readonly IMapper mapper;
        public ProductDelRequestHandler(IBaseRepository<Product> productRepository, IMapper mapper)
        {
            this.productRepository = productRepository;
            this.mapper = mapper;
        }
        public Task<APIResult> Handle(ProductDelRequestModel request, CancellationToken cancellationToken)
        {
            APIResult result = new APIResult();
            result.Code = EnumCode.DelSuccess;
            result.Msg = "删除成功";

            for (int i = 0; i < request.ProductId.Split(',').Length; i++)
            {

                int id = Convert.ToInt32(request.ProductId.Split(",")[i]);
                var res = productRepository.GetAll().Where(x => x.ProductId == id).ToList();
                foreach (var item in res)
                {
                    item.IsDel = true;
                }
                productRepository.UpdateRange(res);
            }


            return Task.FromResult(result);
        }
    }
    public class ProductDelRequestModel:IRequest<APIResult>
    {
        public string ProductId { get; set; }
    }
}
